package com.hyc.www.provider;

import com.hyc.www.factory.ServiceProxyFactory;
import com.hyc.www.model.dto.ServiceResult;
import com.hyc.www.model.po.Ticket;
import com.hyc.www.model.po.User;
import com.hyc.www.provider.annotation.Action;
import com.hyc.www.provider.annotation.ActionProvider;
import com.hyc.www.provider.constant.RequestMethod;
import com.hyc.www.provider.constant.WebPage;
import com.hyc.www.service.TicketService;
import com.hyc.www.service.constants.Status;
import com.hyc.www.service.impl.TicketServiceImpl;
import com.hyc.www.util.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.util.List;

/**
 * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
 * @description 用于提供抢票功能
 * @date 2019-07-17 16:30
 */
@ActionProvider(path = "/ticket")
public class TicketProvider extends BaseProvider {

    private final TicketService ticketService = (TicketService) new ServiceProxyFactory().getProxyInstance(new TicketServiceImpl());

    /**
     * 用于抢票
     *
     * @param req
     * @param resp
     * @return
     * @name buyTicket
     * @notice none
     * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
     * @date 2019-07-17
     */
    @Action(method = RequestMethod.BUY_DO)
    public void buyTicket(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BigInteger ticketId = new BigInteger(req.getParameter("ticket_id"));
        String uid = req.getParameter("user_id");
        BigInteger userId;
        if (uid == null) {
            User user = (User) req.getSession().getAttribute("login");
            userId = user.getId();
        } else {
            userId = new BigInteger(uid);
        }
        ticketService.buyTicket(ticketId, userId);
        req.getRequestDispatcher(WebPage.INDEX_JSP.toString()).forward(req, resp);
    }

    /**
     * 用于退票
     *
     * @param req
     * @param resp
     * @return
     * @notice none
     * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
     * @date 2019-07-17
     */
    @Action(method = RequestMethod.DELETE_DO)
    public void cancelOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BigInteger orderId = new BigInteger(req.getParameter("order_id"));
        ticketService.cancelOrder(orderId);
        req.getRequestDispatcher(WebPage.INDEX_JSP.toString()).forward(req, resp);
    }


    /**
     * 用于查询车票
     *
     * @param req
     * @param resp
     * @return
     * @name buyTicket
     * @notice none
     * @author <a href="mailto:kobe524348@gmail.com">黄钰朝</a>
     * @date 2019-07-17
     */
    @Action(method = RequestMethod.LIST_DO)
    public void listTicket(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("login");
        Ticket ticket = (Ticket) BeanUtils.toObject(req.getParameterMap(), Ticket.class);
        String scope = req.getParameter("scope");
        int page = Integer.parseInt(req.getParameter("page"));
        ServiceResult result;
        if ("mine".equalsIgnoreCase(scope)) {
            result = ticketService.listTicketOrder(user.getId());
            List list = (List) result.getData();
            req.setAttribute("order_list", list);
        } else if("record".equalsIgnoreCase(scope)){
            result = ticketService.listTicketRecord(user.getId());
            List list = (List) result.getData();
            req.setAttribute("record_list",list);
        }else {
            result = ticketService.listTicket(ticket, page);
            List list = (List) result.getData();
            req.setAttribute("ticket_list", list);
        }
        req.setAttribute("message", result.getMessage());
        req.setAttribute("data", result.getData());
        if (Status.ERROR.equals(result.getStatus())) {
            req.getRequestDispatcher(WebPage.ERROR_JSP.toString()).forward(req, resp);
        } else {
            req.getRequestDispatcher(WebPage.INDEX_JSP.toString()).forward(req, resp);
        }
    }

}
